package com.hele.sts.action.admin;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hele.sts.dao.*;
import com.hele.sts.pub.encrypt.BCryptPasswordEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.pub.CodeCategory;
import com.hele.sts.pub.CodeUtils;
import com.hele.sts.pub.StringUtil;
import com.hele.sts.webwork.PageAction;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.beans.factory.annotation.Autowired;

public class QUserManagerAction
  extends PageAction
{
  static Logger logger = LoggerFactory.getLogger(QUserManagerAction.class);
  public static final String LIST = "list";
  public static final String EDIT = "edit";
  public static final String UPDATE = "update";
  public static final String VIEW = "view";
  public static final String DELETE = "del";
  public static final String RESTART = "restart";
  String userId;
  String groupId;
  String userName;
  String passwd;
  String yhlx;
  Date khrq;
  Date cxrq;
  Date ghrq;
  String cxqx;
  String dyqx;
  String bz;
  String errorInfo;
  List qUserList;
  String action;
  String status;
  String realName;
  String yhsf;
  String dyfs;
  //xuning add 20141212
  String cxmd;
  QUser qUser;
  List cxqxList;

  @Autowired
  private XtLogDao xtLogDao;

  /**
 * @return the qUser
 */
public QUser getQUser() {
	return qUser;
}

/**
 * @param qUser the qUser to set
 */
public void setQUser(QUser qUser) {
	this.qUser = qUser;
}



/**
 * @return the cxqxList
 */
public List getCxqxList() {
	return cxqxList;
}

/**
 * @param cxqxList the cxqxList to set
 */
public void setCxqxList(List cxqxList) {
	this.cxqxList = cxqxList;
}

@Override
public String execute()
    throws Exception
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    String loginName = (String)session.get("AdminUser.loginName");
    if ((loginName == null) || (loginName.length() < 1))
    {
      setErrorInfo("没有登录或登录超时！");
      return "error";
    }
    if ((this.action == null) || ("list".equalsIgnoreCase(this.action))) {
      return super.execute();
    }
    if ("update".equalsIgnoreCase(this.action)) {
      return update();
    }
    if ("view".equalsIgnoreCase(this.action)) {
      return view();
    }
    if ("edit".equalsIgnoreCase(this.action))
    {
      if (!doCheckInput()) {
        return "error";
      }
      return edit();
    }
    if("modify".equalsIgnoreCase(this.action)){
    	return modify();
    }
    if ("del".equalsIgnoreCase(this.action)) {
      return delete();
    }
    if ("restart".equalsIgnoreCase(this.action)) {
      return restart();
    }
    return "none";
  }
  
  private String modify() {
	this.qUser = QUserDao.load(this.userId);
	//this.qEkUser = QEkUserDao.load(this.userId);
    this.cxqxList = BMCxqxDao.getCxqxList();
    Collections.reverse(cxqxList);
	return "modify";
}

public String restart()
  {
    QUser bean = QUserDao.load(this.userId);
    if (bean == null)
    {
      setErrorInfo("该用户不存在");
      return "error";
    }
    bean.setStatus("1");
    boolean b = QUserDao.update(bean);
    if (!b)
    {
      setErrorInfo("更新旧用户操作失败");
      return "error";
    }
    logger.debug("success");
    return "restart";
  }
  //用户登记
  public String update()
  {
      ActionContext ctx = ActionContext.getContext();
      Map session = ctx.getSession();
      User adminUser = (User)session.get(("AdminUser"));
    QUser bean = QUserDao.load(this.userId);
    if (bean == null)
    {
      setErrorInfo("该用户不存在");
      return "error";
    }
    if ((this.userName != null) && (this.userName.length() > 0)) {
      bean.setUserName(this.userName);
    }
    if ((this.groupId != null) && (this.groupId.length() > 0)) {
        bean.setGroupId(this.groupId);
      }else{
    	  bean.setGroupId("0");
      }
    if ((this.bz != null) && (this.bz.length() > 0)) {
      bean.setBz(this.bz);
    }
    if ((this.cxqx != null) && (this.cxqx.length() > 0)) {
      bean.setCxqx(this.cxqx);
    }
    if ((this.dyqx != null) && (this.dyqx.length() > 0)) {
      bean.setDyqx(this.dyqx);
    }
    if ((this.status != null) && (this.status.length() > 0)) {
      bean.setStatus(this.status);
    }
    if ((this.realName != null) && (this.realName.length() > 0)) {
      bean.setRealName(this.realName);
    }
    if ((this.passwd != null) && (this.passwd.length() > 0)) {
      BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
      String encodedPassword = passwordEncoder.encode(passwd);
        bean.setPasswd(encodedPassword);
      }
    if ((this.yhsf != null) && (this.yhsf.length() > 0)) {
      bean.setYhsf(this.yhsf);
    }
    if ((this.dyfs != null) && (this.dyfs.length() > 0))
    {
      try
      {
        Integer.parseInt(this.dyfs);
      }
      catch (NumberFormatException e)
      {
        this.dyfs = "0";
      }
      bean.setDyfs(this.dyfs);
    }
    //xuning add 20141212
    if ((this.cxmd != null) && (this.cxmd.length() > 0)) {
        bean.setCxmd(this.cxmd);
      }
    bean.setDjid(StringUtil.randomStrWithDatetime());
    boolean b = QUserDao.update(bean);
    if (!b)
    {
        xtLogDao.insertLog("业务日志","后管用户登记失败",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 用户登记："+userId+" ",this.getClass().getName());

        setErrorInfo("更新旧用户操作失败");
      return "error";
    }
    this.yhsf = CodeUtils.codeToName(CodeCategory.YHSF, this.yhsf);
    this.cxmd = CodeUtils.codeToName(CodeCategory.CXMD, this.cxmd);
    this.cxqx = CodeUtils.codeToName(CodeCategory.CXQX, this.cxqx);
    xtLogDao.insertLog("业务日志","后管用户登记成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 用户登记："+userId+" ",this.getClass().getName());

      return "success";
  }
  
  public String view()
  {
    return null;
  }
  
  public String edit()
  {
    QUser user = QUserDao.loadBy(this.userName);
    if (user != null)
    {
      setErrorInfo("此用户已经存在!");
      setUserName(this.userName);
      return "error";
    }
    boolean b = QUserDao.insert(createQUser());
    if (!b)
    {
      setErrorInfo("添加新用户操作失败");
      return "error";
    }
    return "edit";
  }
  
  public String delete()
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    User adminUser = (User)session.get(("AdminUser"));

    QUser user = QUserDao.load(this.userId);
    if (user == null)
    {
      setErrorInfo("对应的用户不存在!");
      return "error";
    }
    if ((user.getStatus().equals("2")) && (user.getYhlx().equals("2")))
    {
      setErrorInfo("此用户正在使用中，不能注销");
      return "error";
    }
    user.setStatus("3");
    boolean b = QUserDao.update(user);
    if (!b)
    {
      xtLogDao.insertLog("业务日志","后管用户注销失败",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 用户注销："+userId+" ",this.getClass().getName());

      setErrorInfo("更新旧用户操作失败");
      return "error";
    }
    xtLogDao.insertLog("业务日志","后管用户注销成功",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 用户注销："+userId+" ",this.getClass().getName());

    logger.debug("success");
    return "del_success";
  }
  
  @Override
  public String getHref()
  {
    String href = "";
    if ((this.userName != null) && (this.userName.length() > 0)) {
      href = href + "&userName=" + this.userName;
    }
    if ((this.status != null) && (this.status.length() > 0)) {
      href = href + "&status=" + this.status;
    }
    if ((this.yhlx != null) && (this.yhlx.length() > 0)) {
      href = href + "&yhlx=" + this.yhlx;
    }
    return href;
  }
  
  public boolean doCheckInput()
  {
    if ((this.userName == null) || (this.userName.length() < 1))
    {
      setErrorInfo("用户名称必须填写!");
      return false;
    }
    return true;
  }
  
  @Override
  public int count()
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    String adminDjjg = (String)session.get("AdminUser.djjg");
    while (adminDjjg.endsWith("00")){
      adminDjjg = adminDjjg.substring(0,adminDjjg.length()-2);
    }

    AdminUser adminUser = (AdminUser)session.get("myAdminUser");
    if(!adminUser.hasRole("xtgl")){
      this.yhlx = "2";
    }
    return QUserDao.count(this.userName, this.status, this.yhlx, adminDjjg);
  }
  
  @Override
  public String list()
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    String adminDjjg = (String)session.get("AdminUser.djjg");
    while (adminDjjg.endsWith("00")){
      adminDjjg = adminDjjg.substring(0,adminDjjg.length()-2);
    }
    if(this.yhlx==null||"".equals(yhlx)){
    	//this.yhlx = "2"; //默认外部用户
    }
    AdminUser adminUser = (AdminUser)session.get("myAdminUser");
    if(!adminUser.hasRole("xtgl")){
       this.yhlx = "2";
    }

    this.qUserList = QUserDao.load(this.userName, this.status, this.yhlx, getPageNo(), getPageSize(), adminDjjg);
    return "list";
  }
  
  QUser createQUser()
  {
    QUser bean = new QUser();
    bean.setBz(this.bz);
    bean.setCxqx(this.cxqx);
    bean.setCxrq(this.cxrq);
    bean.setDyqx(this.dyqx);
    bean.setGhrq(this.ghrq);
    bean.setGroupId(this.groupId);
    bean.setKhrq(this.khrq);
    bean.setPasswd(this.passwd);
    bean.setStatus("1");
    bean.setUserName(this.userName);
    if ((this.userId == null) || (this.userId.length() < 1)) {
      this.userId = StringUtil.createUserId();
    }
    logger.debug(this.userId);
    bean.setUserId(this.userId);
    bean.setUserName(this.userName);
    bean.setYhlx(this.yhlx);
    return bean;
  }
  
  public String getUserId()
  {
    return this.userId;
  }
  
  public void setUserId(String userId)
  {
    this.userId = userId;
  }
  
  public String getGroupId()
  {
    return this.groupId;
  }
  
  public void setGroupId(String groupId)
  {
    this.groupId = groupId;
  }
  
  public String getUserName()
  {
    return this.userName;
  }
  
  public void setUserName(String userName)
  {
    this.userName = userName;
  }
  
  public String getPasswd()
  {
    return this.passwd;
  }
  
  public void setPasswd(String passwd)
  {
    this.passwd = passwd;
  }
  
  public String getYhlx()
  {
    return this.yhlx;
  }
  
  public void setYhlx(String yhlx)
  {
    this.yhlx = yhlx;
  }
  
  public Date getKhrq()
  {
    return this.khrq;
  }
  
  public void setKhrq(Date khrq)
  {
    this.khrq = khrq;
  }
  
  public Date getCxrq()
  {
    return this.cxrq;
  }
  
  public void setCxrq(Date cxrq)
  {
    this.cxrq = cxrq;
  }
  
  public Date getGhrq()
  {
    return this.ghrq;
  }
  
  public void setGhrq(Date ghrq)
  {
    this.ghrq = ghrq;
  }
  
  public String getCxqx()
  {
    return this.cxqx;
  }
  
  public void setCxqx(String cxqx)
  {
    this.cxqx = cxqx;
  }
  
  public String getDyqx()
  {
    return this.dyqx;
  }
  
  public void setDyqx(String dyqx)
  {
    this.dyqx = dyqx;
  }
  
  public String getBz()
  {
    return this.bz;
  }
  
  public void setBz(String bz)
  {
    this.bz = bz;
  }
  
  public String getErrorInfo()
  {
    return this.errorInfo;
  }
  
  public void setErrorInfo(String errorInfo)
  {
    this.errorInfo = errorInfo;
  }
  
  public String getAction()
  {
    return this.action;
  }
  
  public void setAction(String action)
  {
    this.action = action;
  }
  
  public List getQUserList()
  {
    return this.qUserList;
  }
  
  public void setQUserList(List qUserList)
  {
    this.qUserList = qUserList;
  }
  
  public String getStatus()
  {
    return this.status;
  }
  
  public void setStatus(String status)
  {
    this.status = status;
  }
  
  public String getRealName()
  {
    return this.realName;
  }
  
  public void setRealName(String realName)
  {
    this.realName = realName;
  }
  
  public String getYhsf()
  {
    return this.yhsf;
  }
  
  public void setYhsf(String yhsf)
  {
    this.yhsf = yhsf;
  }
  
  public String getDyfs()
  {
    return this.dyfs;
  }
  
  public void setDyfs(String dyfs)
  {
    this.dyfs = dyfs;
  }

/**
 * @return the cxmd
 */
public String getCxmd() {
	return cxmd;
}

/**
 * @param cxmd the cxmd to set
 */
public void setCxmd(String cxmd) {
	this.cxmd = cxmd;
}
  
}
